/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.web.mvc.validation;

import javax.servlet.ServletException;

import org.merak.core.web.HttpRequest;
import org.merak.core.web.HttpResponse;
import org.merak.core.web.WebParameter;
import org.merak.core.web.mvc.annotation.NotNull;

public class NotNullValidator extends BatchValidator {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private TemplateMessage message       = null;
	private TemplateMessage smallParamMsg = null;
	private TemplateMessage largeParamMsg = null;

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	public NotNullValidator(NotNull annotation) {

		// Initialize super class BatchValidator
		super(annotation.parameters(),annotation.view());

		// Attributes
		this.message       = new TemplateMessage(annotation.message());
		this.smallParamMsg = new TemplateMessage(annotation.smallParamMsg());
		this.largeParamMsg = new TemplateMessage(annotation.largeParamMsg());

	}

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	@Override
	public boolean validate(HttpRequest request,HttpResponse response)
		throws ServletException
	{
		// Auxiliary
		WebParameter param  = null;
		long         length = 0;

		for (TemplateParameter template : this.getParameters()) {
			// Retrieve the parameter's length
			param  = request.getWebParameter(template.getName());
			length = param.getLength();

			// Show an Alert if parameter is Null
			if (param.isNull()) {
				this.throwNotNullMessage(template,this.message,request);
				return false;
			}

			// Throw a message if parameter is too small
			if ( length < template.getMinLength() ) {
				this.throwOutOfBoundMessage(template,this.smallParamMsg,request,template.getMinLength());
				return false;
			}

			// Throw a message if parameter is too large
			if ( length > template.getMaxLength() ) {
				this.throwOutOfBoundMessage(template,this.largeParamMsg,request,template.getMaxLength());
				return false;
			}
		}
		return true;

	}
	//****************************************************************************
	protected void throwNotNullMessage(TemplateParameter p,TemplateMessage message,HttpRequest request)
		throws ServletException
	{

		// a) Throw an exception if view is null
		if ( this.getView().isEmpty() ) throw new ServletException(
			message.getMessage("'"+p+"'")
		);
		// b) Throw a message if view is defined
		request.setAttribute(
			message.getVariable(),
			message.getMessage("'"+p+"'")
		);

	}
	//****************************************************************************
	protected void throwOutOfBoundMessage(TemplateParameter p,TemplateMessage message,HttpRequest request,long limit)
		throws ServletException
	{

		// a) Throw an exception if view is null
		if ( this.getView().isEmpty() ) throw new ServletException(
			message.getMessage("'"+p+"'",limit)
		);
		// b) Throw a message if view is defined
		request.setAttribute(
			message.getVariable(),
			message.getMessage("'"+p+"'",limit)
		);

	}
	//****************************************************************************
}
